<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="900px"
      :before-close="cancel"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form :model="form" ref="formRef" class="demo-dynamic" size="mini" :disabled="flag">
        <el-form-item label="路径类型" label-width="80px">
          <el-radio v-model="form.isDepPath" label="绝对路径">绝对路径</el-radio>
          <el-radio v-model="form.isDepPath" label="相对路径">相对路径</el-radio>
        </el-form-item>
        <el-form-item
          label="路径"
          prop="startPath"
          label-width="80px"
          :rules="{ required: true, validator: this.validateSuffix, trigger: 'blur' }"
        >
          <el-input style="width:240px" v-model="form.startPath"></el-input>
          <span style="margin-left:10px;color:#beb9b9">例：D:/test/test/test.exe 或 /test/test.exe</span>
        </el-form-item>
        <!-- <el-form-item
          label="软件全称"
          prop="exeName"
          label-width="80px"
          :rules="{ required: true, message: '软件全称不能为空', trigger: 'blur' }"
        >
          <el-input style="width:240px" v-model="form.exeName"></el-input>
          <span style="margin-left:10px;color:#beb9b9">例：test.exe</span>
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer" v-if="!flag">
        <el-button type="primary" @click="saveForm">保 存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  props: {
    flag: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      form: {
        isDepPath: '绝对路径',
        startPath: '',
        exeName: ''
      },
      // 弹窗控制
      open: false,
      // 弹窗标题
      title: '',

    }
  },
  methods: {
    // 打开软件启动录入弹窗
    openDialog (data) {
      this.title = '软件启动路径录入'
      this.open = true
      // data 不为空 回显数据
      if (!this.validatenull(data)) {
        this.form = Object.assign({}, data)
      }
    },
    // 关闭弹窗
    cancel () {
      this.form = this.$options.data().form
      this.open = false
    },

    // 保存方法
    saveForm () {
      this.$refs['formRef'].validate((valid) => {
        if (valid) {
          this.$emit('saveStartSoftwareForm', JSON.parse(JSON.stringify(this.form)))
          this.form = this.$options.data().form
          this.open = false
        }
      })

    },
    // 校验后缀是否是exe结尾
    validateSuffix (rule, value, callback) {
      if (this.validatenull(value)) {
        callback(new Error("路径不能为空"));
      }
      if (!value.endsWith('.exe')) {
        callback(new Error("路径必须.exe结尾"));
      }
      callback();
    },
  }
}
</script>

<style lang="scss" scoped>
.pathDivider {
  margin: 12px 0;
}
</style>